package edu.tjufe.llp.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.tjufe.llp.common.Result;
import edu.tjufe.llp.entity.Car;
import edu.tjufe.llp.entity.ChargeArea;
import edu.tjufe.llp.entity.Station;
import edu.tjufe.llp.service.IStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 充电桩Controller
 *
 * @author llp
 * @date 2022-12-23
 */
@RestController
@RequestMapping("/station")
public class StationController
{
    @Autowired
    private IStationService stationService;

    /**
     * 查询充电桩列表
     */
    @GetMapping("/list")
    public Result findPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(defaultValue = "") String stationCode,
                           @RequestParam(required = false) Integer areaId,
                           @RequestParam(defaultValue = "") String state
                           ){
        Page<Station> page = stationService.findPage(new Page<>(pageNum,pageSize),stationCode,areaId,state);
        return Result.success(page);
    }

    /**
     * 查询充电桩总列表
     */
    @GetMapping("/stationTotal")
    public Result findAll(Station station) {
        List<Station> list = stationService.selectStationList(station);
        return Result.success(list);
    }

    /**
     * 导出充电桩列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Station station)
//    {
//        List<Station> list = stationService.selectStationList(station);
//        ExcelUtil<Station> util = new ExcelUtil<Station>(Station.class);
//        util.exportExcel(response, list, "充电桩数据");
//    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<Station> list = stationService.list();
        // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("stationId", "充电桩id");
        writer.addHeaderAlias("stationCode", "编码");
        writer.addHeaderAlias("chargeArea", "区域");
        writer.addHeaderAlias("state", "状态");
        writer.addHeaderAlias("remark", "备注");

        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("充电桩信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }

    /**
     * 获取充电桩详细信息
     */
    @GetMapping(value = "/{stationId}")
    public Result getInfo(@PathVariable("stationId") Integer stationId)
    {
        return Result.success(stationService.selectStationByStationId(stationId));
    }

    /**
     * 新增充电桩
     */
    @PostMapping
    public Result add(@RequestBody Station station)
    {
        return Result.toJudge(stationService.insertStation(station));
    }

    /**
     * 修改充电桩
     */
    @PutMapping
    public Result edit(@RequestBody Station station)
    {
        return Result.toJudge(stationService.updateStation(station));
    }

    /**
     * 删除充电桩
     */
    @DeleteMapping("/{stationIds}")
    public Result remove(@PathVariable Integer[] stationIds)
    {
        return Result.toJudge(stationService.deleteStationByStationIds(stationIds));
    }
}
